package com.campus.api.interceptor;


import cn.hutool.core.util.StrUtil;
import com.campus.api.util.JwtHelper;
import com.campus.api.util.UserContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class UserInfoInterceptors  implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取登录用户信息
        String userIdJWT = request.getHeader("user-info");
        if (userIdJWT != null && StrUtil.isNotBlank(userIdJWT)) {
            UserContext.setUserId(JwtHelper.getUserId(userIdJWT));
        }
//        if(StrUtil.isBlank(userId)){
//            UserContext.setUserId(Integer.valueOf(userId));
//        }
    	return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
    	UserContext.removeUserId();
    }

}
